package command.login;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import model.Questionario;
import model.Respondente;

import command.ICommand;

import dao.QuestionarioDAO;
import dao.RespondenteDAO;

public class LogarRespondenteCommand implements ICommand{

	public String execute(HttpServletRequest request) throws ServletException,IOException {
		HttpSession session = request.getSession();
		String matricula = request.getParameter("matricula");
		String senha = request.getParameter("senha");
		Respondente respondenteLogado = null;
		RespondenteDAO dao = new RespondenteDAO();
		QuestionarioDAO questDAO = new QuestionarioDAO();
		Questionario questionario = new Questionario();
		try {
			respondenteLogado = dao.isUsuarioValido(matricula, Integer.toString(senha.hashCode()));
			questionario = questDAO.getQuestionarioRespondente(respondenteLogado.getCodigo());
			respondenteLogado.setQuestionario(questionario);
			session.setAttribute("respondenteLogado", respondenteLogado);
			return "/paginas/respondente/logar_respondente.jsp";
		} catch (SQLException e) {
			e.printStackTrace();
			return "#";
		}
	}

}
